package boogui.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.db.MovieRegistrySQL;
import boogui.movie.MovieVO;

public class MovieRegistryDAO implements IMovieRegistryDAO{
	IBooguiDAO bdao = null;
	MovieRegistrySQL sql = null;

	public MovieRegistryDAO() {
		bdao = new BooguiDAO();
		sql = new MovieRegistrySQL();
	}

	// 영화 정보 등록
	@Override
	public boolean insertMovie(MovieVO m, String path) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		InputStream fis = null;
		boolean result = false;
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getInsertMovieSQL());
			
			File image = new File(path);
			fis = new FileInputStream(image);
			int ilen = (int) image.length();
			
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getMovgenre1());
			pstmt.setString(3, m.getMovgenre2());
			pstmt.setString(4, m.getMovdirector());
			pstmt.setString(5, m.getMovactor1());
			pstmt.setString(6, m.getMovactor2());
			pstmt.setString(7, m.getMovactor3());
			pstmt.setString(8, m.getMovrating());
			pstmt.setString(9, m.getMovsynop());
			pstmt.setString(10, m.getMovruntime());
			pstmt.setString(11, m.getMovplaydate());
			pstmt.setString(12, m.getMovisplay());
			pstmt.setString(13, m.getMovresv());		
			pstmt.setBinaryStream(14, fis, ilen);;
			pstmt.executeUpdate();			
			
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null) try {fis.close();} catch (IOException e) {}
			bdao.closeDatabase(conn, pstmt, null);
		}
		return result;
		}

}
